package controller

import (
	"github.com/gin-gonic/gin"
	"k9-panel/app/adapter/http/internal/convert"
	"k9-panel/app/adapter/internal"
	"k9-panel/app/model"
	"k9-panel/app/response"
	"k9-panel/app/service"
	"net/http"
)

var (
	UserToken *UserTokenController
)

func init() {
	UserToken = new(UserTokenController)
}

type UserTokenController struct {
	common
}

func (t *UserTokenController) Update(c *gin.Context) {
	var (
		baseAdd internal.UserTokenUpdateParam
	)
	if err := c.ShouldBind(&baseAdd); err != nil {
		panic(err)
	}
	item := &model.UserToken{
		Desc:              baseAdd.Desc,
		Value:             baseAdd.Value,
		ManagerProjectIds: baseAdd.ProjectIds,
		ManagerEnvIds:     baseAdd.EnvIds,
	}
	item.ID = baseAdd.Id
	create, err := service.UserToken.Update(item)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(create))
	return
}

func (t *UserTokenController) Delete(c *gin.Context) {
	var (
		baseRead model.UserToken
	)
	if err := c.ShouldBind(&baseRead); err != nil {
		panic(err)
	}
	configs, err := service.UserToken.Delete(&baseRead)
	if err != nil {
		panic(err)
	}
	c.JSON(http.StatusAccepted, response.Success(configs))
	return
}

func (t *UserTokenController) List(c *gin.Context) {
	var (
		baseAdd internal.ListUserTokenParam
	)
	if err := c.ShouldBind(&baseAdd); err != nil {
		panic(err)
	}
	create, err := service.UserToken.List()
	if err != nil {
		panic(err)
	}

	rsp := convert.UserTokenList2UserTokenRsp(create)
	c.JSON(http.StatusAccepted, response.Success(rsp))
	return
}
